home *** CD-ROM | disk | FTP | other *** search
- /* cvsave.c
- * routine to save or restore the user's game
- * only one game per user name is allowed in this version
- * what is saved:
- * a time-stamp of the program version (not the game)
- * all non-stack variables from cvmain.c
- * all variable info from cvlocs.c, cvobj.c
- *************************************************************************/
-
- #include <string.h>
- #include <stdio.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include "cvobj.h"
- #include "cvlocs.h"
-
- #define DIRNAME "/usr/games/lib/crystal/"
- #define DIRLEN (sizeof DIRNAME)
- extern char *cuserid();
- extern char emain, datastart;
- extern int saved;
- static char savename[DIRLEN+L_cuserid] = DIRNAME;
- static unsigned datalen ;
- static unsigned conlen = sizeof(struct cvloc *) + sizeof(struct conn *);
-
- long ptime = -1; /* program time-stamp */
-
- void
- cvsave()
- { register struct cvloc *curloc;
- register struct cvobj *curobj;
- auto char userid[L_cuserid];
- auto struct stat buf;
- auto int save;
-
- datalen = &emain-&datastart;
-
- if (cuserid(userid) == NULL) {
- puts("\nCannot save because I don't know who you are!");
- return;
- }
-
- (void) strcpy(savename+DIRLEN-1, userid);
- if (stat(savename,&buf) == -1) {
- if (errno != ENOENT) {
- puts("\nCannot save because of directory trouble\n");
- return;
- }
- } else {
- if (yes(93,0,0)) {
- if (unlink(savename) == -1) {
- printf("\nCannot unlink %s\n",savename);
- return;
- }
- } else { return; }
- }
- if ((save = open(savename,O_WRONLY|O_EXCL|O_CREAT,0600)) == -1) {
- printf("\nCannot create %s\n",savename);
- return;
- }
- if (write(save,&ptime,sizeof(long)) == -1
- || write(save,&datastart,datalen) == -1) {
- perror("Saving cave:");
- close(save);
- unlink(savename);
- return;
- }
- for (curobj = cvobj; curobj->desc != NULL; curobj++) {
- if (write(save, &(curobj->prop), sizeof(int)) == -1
- || write(save, &(curobj->conn1.where),conlen) == -1
- || write(save, &(curobj->conn2.where),conlen) == -1) {
- perror("Saving cave objects");
- close(save);
- unlink(savename);
- return;
- }
- }
- for (curloc = cvloc; curloc->travel != NULL; curloc++) {
- if (write(save, &(curloc->abb), sizeof(int)) == -1
- || write(save, &(curloc->atloc.where),conlen) == -1) {
- perror("Saving cave places");
- close(save);
- unlink(savename);
- return;
- }
- }
- (void) close(save);
- return;
- }
-
- void
- cvrest()
- { register struct cvloc *curloc;
- register struct cvobj *curobj;
- auto char userid[L_cuserid];
- auto int save;
- auto long ftime = -1;
-
- datalen = &emain-&datastart;
-
- if (cuserid(userid) == NULL) {
- puts("\nCannot restore because I don't know who you are!");
- return;
- }
-
- (void) strcpy(savename+DIRLEN-1, userid);
-
- if ((save = open(savename,O_RDONLY)) == -1) {
- printf("\nCannot find %s\n",savename);
- return;
- }
- if (read(save,&ftime,sizeof(long)) != sizeof(long)
- #ifdef XVERSION
- || ftime != ptime
- #endif
- || read(save,&datastart,datalen) != datalen) {
- if (ftime == -1) {
- perror("Restoring cave:");
- } else {
- puts("\nSave file not created by this version");
- }
- close(save);
- unlink(savename);
- return;
- }
- for (curobj = cvobj; curobj->desc != NULL; curobj++) {
- if (read(save, &(curobj->prop), sizeof(int)) != sizeof(int)
- || read(save, &(curobj->conn1.where),conlen) != conlen
- || read(save, &(curobj->conn2.where),conlen) != conlen) {
- perror("Restoring cave objects");
- close(save);
- unlink(savename);
- return;
- }
- }
- for (curloc = cvloc; curloc->travel != NULL; curloc++) {
- if (read(save, &(curloc->abb), sizeof(int)) != sizeof(int)
- || read(save, &(curloc->atloc.where),conlen) != conlen) {
- perror("Restoring cave places");
- close(save);
- unlink(savename);
- return;
- }
- }
- if (read(save, &ftime, 1) != 0) {
- puts("\nNot end of file");
- close (save);
- unlink(savename);
- exit(1);
- }
- (void) close(save);
- if (saved == 1) saved = -1;
- return;
- }
-